home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / GNUUCP_2 / SOURCE / SYSDEP.C < prev    next >
Text File  |  1990-07-16  |  20KB  |  952 lines

  1. /*
  2.  * @(#)sysdep.mac 1.2 87/09/23    Copyright 1987 Free Software Foundation, Inc.
  3.  *gnu
  4.  * Copying and use of this program are controlled by the terms of the
  5.  * GNU Emacs General Public License.
  6.  */
  7.  
  8. #ifndef lint
  9. char sysdep_version[] = "@(#)sysdep.mac gnuucp Version Fort Pond Research-2.6";
  10. #endif
  11.  
  12. /*
  13.  * Created 22 September 1987 by John Gilmore, 
  14.  * using code from sun!seismo!psc!jsl (John Labovitz).
  15.  * His comments:
  16.  
  17.     1. Logging cleanup.  It makes it somewhat easier to make the
  18.        right time and date for whatever OS you're using.  I made it
  19.        more modular.
  20.     2. Changes in the login routine to let it be more like real Unix
  21.        systems.  I made a special routine that reads a line from the
  22.        input port, and *then* checks it, instead of only checking each
  23.        character as it comes in.  Also, it handles ^D, which my Unix
  24.        systems uses to make sure there's a prompt.  Doesn't handle break.
  25.  
  26. This needs some more work on the Mac side, like letting the user abort
  27. uuslave.  Right now you have to reset the machine!
  28.  
  29. Hope this is useful.  I'm really interesting in running this thing, although
  30. I'm not quite sure how I'm going to configure it (like how to read mail on
  31. the Mac).  But soon I'll be "potomac!sly!jsl".  What I might do is actually
  32. run this as a server on a PC, connected to the Unix machine via RS232, then
  33. write a program on the Mac that can access the PC via Appletalk (aon the
  34. MacBridge card) and get the mail.  That way, any of our 30 macs can get mail
  35. without being connected to Unix... Dreams.
  36.  
  37.  */
  38.  
  39. #include "includes.h"
  40. #include <pascal.h>
  41. #include "uucp.h"
  42.  
  43. #define    CONTROL    "gnuucp.ctl"
  44. int abort_key_pressed(void);
  45. void HandleEvent (EventRecord *, int);
  46. int gnuucp_cleaned_up = 0;
  47.  
  48.  
  49. /*
  50.  * Exported variables
  51.  */
  52. char     sysdep_control[] = CONTROL;
  53.  
  54. /*
  55.  * Open the serial line for an incoming call.
  56.  * Argument of NULL or empty string means stdin.
  57.  * FIXME, baud rate should be settable here?
  58.  */
  59. char inbuf[4096];
  60.  
  61. SPortSel OpenPort = NO_PORT;
  62. int inRefNum;
  63. int outRefNum;
  64.  
  65. int
  66. openline(ttynam, baud)
  67.     char    *ttynam;
  68.     int    baud;
  69. {
  70.  
  71.     int baud_set;
  72.     int compos;
  73.     int err;
  74.     SerShk Opts;
  75.     compos = strcmp(".B", ttynam);
  76.     if (compos == -1) 
  77.         {
  78.             OpenPort = sPortB;
  79.             inRefNum = BinRefNum;
  80.             outRefNum = BoutRefNum;
  81.             }
  82.         else
  83.         {
  84.             compos = strcmp(".A", ttynam);
  85.             if (compos == -1)
  86.                 {
  87.                     OpenPort = sPortA;
  88.                     inRefNum = AinRefNum;
  89.                     outRefNum = AoutRefNum;
  90.                     }
  91.             else
  92.                 {
  93.                     logit("Unknown port.", ttynam);
  94.                     sigint();
  95.                     }
  96.             }
  97.                 
  98.         
  99.     Opts.fXOn = 0;
  100.     Opts.fCTS = 1;
  101.     Opts.xOn = 0;
  102.     Opts.xOff = 0;
  103.     Opts.errs = 0;
  104.     Opts.evts = 0;
  105.     Opts.fInX = 0;
  106.     Opts.fDTR = 1;
  107.     switch (baud)
  108.         {
  109.             case 300:
  110.                 baud_set = baud300;
  111.                 break;
  112.             case 1200:
  113.                 baud_set = baud1200;
  114.                 break;
  115.             case 2400:
  116.                 baud_set = baud2400;
  117.                 break;
  118.             case 3600:
  119.                 baud_set = baud3600;
  120.                 break;
  121.             case 4800:
  122.                 baud_set = baud4800;
  123.                 break;
  124.             case 7200:
  125.                 baud_set = baud7200;
  126.                 break;
  127.             case 9600:
  128.                 baud_set = baud9600;
  129.                 break;
  130.             case 19200:
  131.                 baud_set = baud19200;
  132.                 break;
  133.             case 57600:
  134.                 baud_set = baud57600;
  135.                 break;
  136.             default:
  137.                 return(EOF);
  138.                 break;
  139.                 }
  140.     if (RAMSDOpen(OpenPort) != noErr)
  141.         return(EOF);
  142.     SerReset(inRefNum, baud_set+data8+stop10);
  143.     SerReset(outRefNum, baud_set+data8+stop10);
  144.     SerSetBuf(inRefNum, inbuf, 4096);
  145.     /* if ((err = SerHShake(inRefNum, &Opts)) != noErr)
  146.         {
  147.             return(EOF);
  148.             }
  149.     if ((err = SerHShake(outRefNum, &Opts)) != noErr)
  150.         {
  151.             printf("First handshake: %ld\n", (long)err);
  152.             return(EOF);
  153.             } */
  154.     gnuucp_cleaned_up = 0;
  155.     return(0);
  156. }
  157.  
  158. /*
  159.  * Open the serial line for an outgoing call.
  160.  */
  161. int
  162. openout(p)
  163.     struct port *p;
  164.     {
  165.     int tty;
  166.     tty = openline(p->devname, p->baud);
  167.     return(tty);
  168. }
  169.  
  170. int
  171. openin(p)
  172.     struct port *p;
  173.     {
  174.     int tty;
  175.     char *ttynam;
  176.     char *modemname;
  177.     int baud;
  178.     ttynam = p->devname;
  179.     modemname = p->modemname;
  180.     baud = p->baud;
  181.     tty = openline(ttynam, baud);
  182.     if (strcmp(modemname, "none") != 0)
  183.         {
  184.             xwrite(0, "+++\r", 3);
  185.             gnusleep(4);
  186.             xwrite(0, "ats0=2\r", 7);
  187.             }
  188.     return(tty);
  189. }
  190. /*
  191.  * Basement level I/O routines
  192.  *
  193.  * xwrite() writes a character string to the serial port
  194.  * xgetc() returns a character from the serial port, or an EOF for timeout.
  195.  * sigint() restores the state of the serial port on exit.
  196.  * hangup() hangs up the serial port (e.g. drop DTR).
  197.  */
  198.  
  199. void
  200. sigint()
  201. {
  202.     exit(1);
  203.     }
  204.  
  205. xwrite(dummy, buf, ctr)
  206. int dummy;
  207. char *buf;
  208. int ctr;
  209. {
  210.     long count;
  211.     
  212.     count = ctr;
  213.     
  214.     HandleEvents();
  215.     if (FSWrite(outRefNum, &count, buf) != noErr)
  216.         return(EOF);
  217.     return((int)count);
  218. }
  219.  
  220.  
  221. xgetc()
  222. {
  223.     unsigned char data;
  224.     long count;
  225.     int delay;
  226.     long i;
  227.     
  228.     i = TickCount() + (ByteTimeout * 60L);
  229.     count = 0;
  230.     delay = 500;
  231.     while ((TickCount() < i)) 
  232.     {
  233.         SerGetBuf(inRefNum, &count);
  234.         HandleEvents();
  235.         if (count > 0) 
  236.             {
  237.                 count = 1;    /* make sure we only read one byte */
  238.                 if (FSRead(inRefNum, &count, &data) != noErr)
  239.                     return(EOF);
  240.                    else
  241.                     return(data & 0xFF);
  242.                   }
  243.                     else
  244.                    {
  245.                         if (delay == 0)
  246.                             {
  247.                                 gnusleep(1);
  248.                                 delay = 500;
  249.                                 }
  250.                             else
  251.                                 {
  252.                                     delay--;
  253.                                     }
  254.                         }
  255.         }
  256.     return(EOF);
  257. }
  258.  
  259. /*
  260.  * hangup(): hang up the 'fone.
  261.  */
  262. int
  263. hangup(p)
  264. struct port *p;
  265. {
  266.  
  267.     char *modemname;
  268.     modemname = p ->modemname;
  269.     if (strcmp(modemname, "none") != 0)
  270.         {
  271.             gnusleep(1);
  272.             xwrite((int)NULL, "+++\r", 3);
  273.             gnusleep(3);
  274.             xwrite((int)NULL, "ATH\r", 4);
  275.             xwrite((int)NULL, "+++\r", 3);
  276.             gnusleep(1);
  277.             xwrite((int)NULL, "ats0=0\r", 7);
  278.             gnusleep(3);        /* To be sure DTR stays down that long */
  279.             gnuucp_cleaned_up = 1;
  280.             }
  281.     if (OpenPort != NO_PORT);
  282.         RAMSDClose(OpenPort);
  283.     }
  284.         
  285.  
  286.  
  287. /*
  288.  * Create a temporary file name for receiving a file into.
  289.  * "name" is the name we will actually eventually want to use for the file.
  290.  * We currently ignore it, but some OS's that can't move files around
  291.  * easily might want to e.g. put the temp file into the same directory
  292.  * that this file is going into.
  293.  *
  294.  * FIXME:
  295.  * This interface should be able to return a "possible" filename, and
  296.  * be re-called if the name is already in use, to get another.
  297.  * This avoids checking here whether the name is good -- saving system calls.
  298.  */
  299. #define MAXTRIES 10
  300. char *
  301. temp_filename(name)
  302.     register char *name;
  303. {
  304.     static char tname[NAMESIZE];
  305.     int i;
  306.     FILE *fd;
  307.     i = 0;
  308.     if (ourpid == 0)
  309.         ourpid = getpid();
  310.     while (TRUE)
  311.         {
  312.             i++;
  313.             (void) sprintf(tname, "TM.u%d", rand());
  314.             if (access(tname, 0) != 0)
  315.                 break;
  316.             if (i == MAXTRIES) 
  317.                 {
  318.                     printf("can't generate unique file name\n");
  319.                     exit(EXIT_ERR);
  320.                     }
  321.     }
  322.     DEBUG(7, "Using temp file %s\n", tname);
  323.     return tname;
  324. }
  325.  
  326.  
  327. /*
  328.  * Transform a filename from a uucp packet (in Unix format) into a local
  329.  * filename that will work in the local file system.
  330.  *
  331.  * This is a real hack; it puts all the files into one big directory.
  332.  * Everything.  Yuk.
  333.  */
  334. char *
  335. munge_filename(name)
  336.     register char *name;
  337. {
  338.     register char *p;
  339.     static char buffer[NAMESIZE+SLOP];
  340.     int len, hostlen;
  341.  
  342.     DEBUG(7, "Munge_filename  input: %s\n", name);
  343.  
  344.     for (p = name + strlen(name); p != name && *(p-1) != '/'; p--) ;
  345.  
  346.     DEBUG(7, "Munge_filename output: %s\n", p);
  347.     return p;
  348. }
  349.  
  350. /*
  351.  * Uucp work queue scan.
  352.  *
  353.  * gotsome = work_scan(hostname, type);
  354.  * workfile = work_next();
  355.  * void work_done();
  356.  */
  357.  
  358. /*
  359.  * Local variables of the work queue scanner -- not visible to outsiders
  360.  *
  361.  * Workhost and worktype are always null-terminated; keeping their lengths
  362.  * is just a performance hack.  Workprefix is NOT null terminated; various
  363.  * people copy things after it and use the whole string.  Workprefix is
  364.  * exactly workplen long; to append something, strcpy(workprefix+workplen, ...)
  365.  */
  366. #define MAX_TYPE    10
  367. static struct DIR    *workdir = (struct DIR *)NULL;
  368. static struct dirent    *workfile;
  369. static char    workhost[MAX_HOST+1];
  370. static int    workhlen;
  371. static char    worktype[MAX_TYPE+1];
  372. static int    worktlen;
  373. static char    workfirst = 0;
  374. /* FIXME, at the moment this can be local to work_scan() */
  375. static char    workprefix[MAX_TYPE+1+MAX_HOST+1+MAX_TYPE+1+MAX_HOST+6+SLOP];
  376.         /*       D        . hoptoad  / D        . hoptoad  N1234\0 */
  377. static int    workplen;
  378.  
  379.  
  380. void
  381. work_done()
  382. {
  383.     if (workdir)
  384.         closedir(workdir);
  385.     workdir = (struct DIR *) NULL;
  386.     workfile = (struct dirent *) NULL;
  387.     workfirst = 0;
  388. }
  389.  
  390. char *
  391. index(str, ch)
  392. char *str;
  393. char ch;
  394. {
  395.     strchr(str, ch);
  396.     }
  397.     
  398. int
  399. work_scan(host, type)
  400.     char *host;
  401.     char *type;
  402. {
  403.     char *p;
  404.  
  405.     if (!host)
  406.         host = "";
  407.  
  408.     /* If called twice in a row, don't thrash the disk again */
  409.     /* if (strcmp(workhost, host) == SAME &&
  410.         strcmp(worktype, type) == SAME && workfile)
  411.         return 1; */
  412.  
  413.     if (workdir) work_done();        /* Clean up prev call */
  414.  
  415.     workfirst = 1;                /* Initialize for work_next */
  416.     workhlen = strlen(host);
  417.     if (workhlen > sizeof (workhost) -1) 
  418.         {
  419.             printf("WORK_SCAN SIZE RESTRICTION", "workhlen > sizeof(workhost) - 1");
  420.             exit(1);
  421.             }
  422.     strcpy(workhost, host);
  423.     if (workhlen > 7) workhlen = 7;        /* Unix uucp limit */
  424.     worktlen = strlen(type);
  425.     if (worktlen > sizeof (worktype) -1) 
  426.         {
  427.             printf("WORK_SCAN SIZE RESTRICTION", "worktlen > sizeof(worktype) - 1");
  428.             exit(1);
  429.             }
  430.     strcpy(worktype, type);
  431.  
  432.     /* Figure out which subdirectory this class of files is in. */
  433.     /* FIXME: doesn't handle "all D. files" since D.myname is separate. */
  434.     sprintf(workprefix, "%s.%s", worktype, workhost);
  435.     p = munge_filename(workprefix);
  436.     strcpy(workprefix, p);
  437.     p = index(workprefix, ':');
  438.     if (p)
  439.         workplen = 1 + p - workprefix;    /* Prefix ends after '/' */
  440.     else
  441.         workplen = 0;        /* No / in munged; hence no dir */
  442.     workprefix[workplen] = '\0';
  443.     DEBUG(7, "Work prefix is =%s=\n", workprefix);
  444.  
  445.     strcpy(workprefix+workplen, ".");
  446.     workdir = opendir(workprefix);    /* Open whatever directory */
  447.     if (workdir == NULL) {
  448.         DEBUG(0, "Can't open queue directory %s\n", workprefix);
  449.         return 0;            /* No work */
  450.     }
  451.  
  452.     return work_look();
  453. }
  454.  
  455.  
  456. static int
  457. work_look()
  458. {
  459.     int len;
  460.     closedir(workdir);
  461.     opendir(workprefix);
  462.     for (;;) {
  463.         workfile = readdir(workdir);
  464.         if (workfile == NULL) {
  465.             DEBUG(7, "work_look readdir null\n", 0);
  466.             work_done();
  467.             return 0;        /* No work */
  468.         }
  469.         DEBUG(7, "work_look readdir %s\n", workfile->d_name);
  470.  
  471.         /* Is it the right type? */
  472.         if (strncmp(workfile->d_name, worktype, worktlen) == SAME
  473.             && workfile->d_name[worktlen] == '.') {
  474.             /* see if it matches the hostname */
  475.             len = strlen(workfile->d_name);
  476.             /*         "C"        "." "hoptoad" "Xnnnn" */
  477.             if (workhlen == 0 || 
  478.                 (len == worktlen + 1 + workhlen + 5 &&
  479.                  !strncmp(workhost, workfile->d_name+2, workhlen))
  480.                ) {
  481.                 /* Found an entry! */
  482.                 /* FIXME, check grade letter! */
  483.                 DEBUG(7, "work_look found it!\n", 0);
  484.                 return 1;    /* Found work */
  485.             }
  486.         }
  487.     }
  488.     /* NOTREACHED */
  489. }
  490.  
  491.  
  492. char *
  493. work_next()
  494. {
  495.  
  496.     if (!workfirst) {
  497.         if (!workfile || !work_look())
  498.             return (char *)NULL;
  499.     }
  500.     workfirst = 0;
  501.     return workfile->d_name;
  502. }
  503.  
  504. /*
  505.  * Routine to return a string that gives the current date and time, and
  506.  * identifies the current process, if on a multiprocess system.
  507.  */
  508. char *
  509. time_and_pid()
  510. {
  511.     unsigned long clock;
  512.     struct tm *tm;
  513.     static int ourpid = 0;
  514.     static char format[] = "%d/%d-%d:%02d:%02d-%d";
  515.     static char outbuf[sizeof(format)];
  516.  
  517.     (void) time(&clock);
  518.     tm = localtime(&clock);
  519.     if (ourpid == 0)
  520.         ourpid = getpid();
  521.     sprintf(outbuf, format,
  522.         tm->tm_mon+1, tm->tm_mday,
  523.         tm->tm_hour, tm->tm_min, tm->tm_sec,
  524.         ourpid);
  525.     return outbuf;
  526. }
  527.  
  528. int
  529. chdir(ndir)
  530. char *ndir;
  531. {
  532.     WDPBRec    pb;
  533.     int err = 0;
  534.     int vol;
  535.     vol = 0;
  536.     CtoPstr(ndir);
  537.     pb.ioCompletion = 0;
  538.     pb.ioNamePtr = (StringPtr)ndir;
  539.     pb.ioVRefNum = 0;
  540.     pb.ioWDDirID = 0;
  541.     /* err = PBHSetVol(&pb,0); */
  542.     PtoCstr(ndir);
  543.     return(err);
  544.     }
  545.  
  546. #ifdef IGNORED
  547. int
  548. execlp (pgm, arg1, arg2)
  549. char *pgm;
  550. char *arg1;
  551. int arg2;
  552. {
  553.     printf("Going to exec", pgm);
  554.     }
  555. #endif
  556.  
  557. HFileParam Opendir;
  558. char file_name[255];
  559.  
  560. struct DIR*
  561. opendir(path)
  562. char *path;
  563. {
  564.     char tmp_str[255];
  565.     WDPBRec    pb;
  566.     int err = 0;
  567.     int vol;
  568.     vol = 0;
  569.     strcpy(tmp_str, Spool);
  570.     CtoPstr(tmp_str);
  571.     pb.ioCompletion = 0;
  572.     pb.ioNamePtr = (StringPtr)tmp_str;
  573.     pb.ioVRefNum = 0;
  574.     pb.ioWDDirID = 0;
  575.     err = PBOpenWD(&pb,0);
  576.     if (err != noErr)
  577.         return((struct DIR *)0);
  578.     Opendir.ioCompletion = 0;
  579.     Opendir.ioNamePtr = (StringPtr)&file_name;
  580.     Opendir.ioVRefNum = pb.ioVRefNum;
  581.     Opendir.ioFDirIndex = 0;
  582.     Opendir.ioDirID = 0;
  583.     return((struct DIR *)&Opendir);
  584.     }
  585.  
  586. int closedir(dir)
  587. struct DIR *dir;
  588. {
  589.     WDPBRec pb;
  590.     int err;
  591.     pb.ioCompletion = 0;
  592.     pb.ioVRefNum = Opendir.ioVRefNum;
  593.     err = PBCloseWD(&pb, 0);
  594.     return(err);
  595.     }
  596.     
  597. struct dirent current_file;
  598.  
  599. struct dirent*
  600. readdir(dir)
  601. struct DIR *dir;
  602. {
  603.     int err;
  604.     Opendir.ioCompletion = 0;
  605.     Opendir.ioNamePtr = (StringPtr)&file_name;
  606.     Opendir.ioDirID = 0;
  607.     Opendir.ioFDirIndex++;
  608.     err = PBHGetFInfo(&Opendir, 0);
  609.     if (err != noErr)
  610.         return((struct dirent *)0);
  611.     current_file.d_name = (char *)Opendir.ioNamePtr;
  612.     PtoCstr(current_file.d_name);
  613.     return(¤t_file);
  614.     }
  615.  
  616. #ifdef IGNORED
  617. char *strtok(in, look_for)
  618.     char *in;
  619.     char *look_for;
  620.     {
  621.         static char *saved;
  622.         char *tmp, *tmp1;
  623.         if (in != NULL)
  624.             {
  625.                 saved = in;
  626.                 }
  627.         tmp = strpbrk(saved, look_for);
  628.         tmp1 = saved;
  629.         saved = tmp + strspn(tmp, look_for);
  630.         *tmp = '\0';
  631.         return(tmp1);
  632.         }
  633. #endif
  634.  
  635. void
  636. bzero(ptr, bytes)
  637. char *ptr;
  638. unsigned bytes;
  639. {
  640.     memset(ptr, 0, bytes);
  641.     }
  642.  
  643. void
  644. bcopy(to, from, count)
  645. char *to, *from;
  646. int count;
  647. {
  648.     memcpy(from, to, count);
  649.     }
  650.  
  651. #ifdef IGNORED
  652. int system(exe)
  653.     char *exe;
  654.     {
  655.         printf("SYSTEM NOT IMPLEMENTED", exe);
  656.         exit(1);
  657.         }
  658. #endif
  659.         
  660. int
  661. access(file, code)
  662.     char *file;
  663.     int code;
  664.     {
  665.         FILE *exists;
  666.         exists = fopen(file, "r");
  667.         if (exists != 0) 
  668.             {
  669.                 fclose(exists);
  670.                 return(0);
  671.                 }
  672.             else
  673.                 {
  674.                     if (errno == bdNamErr)
  675.                             return(0);
  676.                         else
  677.                             return(1);
  678.                     }
  679.                 }
  680.  
  681.     
  682. /* get a random number for names and other stuff */
  683. random()
  684. {
  685.     int it;
  686.     long tmp;
  687.  
  688.     it = time((unsigned long *)&tmp);
  689.     it &= 0xff;
  690.     return (it);
  691. }
  692.  
  693. /* return the index of c in st. The first char is at 0 */
  694. mindex(st, c)
  695. char c, *st;
  696.  
  697. {
  698.     int i;
  699.  
  700.     for (i = 0; st[i] != '\0'; i++) {
  701.     if (st[i] == c)
  702.         return (i);
  703.     }
  704.     return (-1);
  705. }
  706.  
  707. void
  708. gnuucp_cleanup()
  709.     {
  710.         /* hanup the phone This is abnormal so always do the hayes thing */
  711.         if (!gnuucp_cleaned_up)
  712.             {
  713.                 gnusleep(1);
  714.                 xwrite((int)NULL, "+++", 3);
  715.                 gnusleep(3);
  716.                 xwrite((int)NULL, "ATH\r", 4);
  717.                 xwrite((int)NULL, "+++", 3);
  718.                 gnusleep(1);
  719.                 xwrite((int)NULL, "ats0=0\r", 7);
  720.                 if (OpenPort != NO_PORT)
  721.                     RAMSDClose(OpenPort);
  722.                 gnusleep(3);        /* To be sure DTR stays down that long */
  723.                 gnuucp_cleaned_up = 1;
  724.                 }
  725.         }
  726.  
  727. long eventcounter = 0;
  728. #define EVENTMAX 50
  729.  
  730. void ProcessEvent (void);
  731.  
  732. /* swallow key events looking for Command-. */
  733. void HandleEvents()
  734.     {
  735.         EventRecord event;
  736.         int sys;
  737.         if (eventcounter <= 0)
  738.             {
  739.                     {
  740.                         if (abort_key_pressed() == 1) sigint();
  741.                         GetNextEvent(everyEvent, &event);
  742.                         HandleEvent(&event, sys);
  743.                         }
  744.                 eventcounter = EVENTMAX;
  745.                 }
  746.             else
  747.                 eventcounter--;
  748.             }
  749.  
  750.  
  751. char *currtime()
  752. {
  753.     static char timebuffer[26];
  754.     register char *tp;
  755.     long Time;
  756.     char Year[10];
  757.     DateTimeRec MacTimeRec;
  758.     register Intl1Hndl myHndl;
  759.     register int i;    
  760.         GetDateTime(&Time);
  761.         Secs2Date(Time,&MacTimeRec);
  762.     
  763.         myHndl = (Intl1Hndl)IUGetIntl(1);
  764.         tp = timebuffer;
  765.     
  766.         for (i=1; i<4; i++)
  767.             *tp++ = (*myHndl)->days[MacTimeRec.dayOfWeek-1][i]; /* make day of week */
  768.         *tp++ = ',';
  769.         *tp++ = ' ';
  770.         sprintf(tp,"%02d ",MacTimeRec.day); /* now put in day of month */
  771.         tp += 3;
  772.         for (i=1; i<4; i++)
  773.             *tp++ = (*myHndl)->months[MacTimeRec.month-1][i]; /* make month */
  774.         *tp++ = ' ';
  775.         Year[0] = '\0';
  776.         sprintf(Year, "%d", MacTimeRec.year);
  777.         for (i=2; i<4; i++)
  778.             *tp++ = Year[i]; /* make year */
  779.         *tp++ = ' ';
  780.         sprintf(tp,"%02d:%02d:%02d\n",MacTimeRec.hour,MacTimeRec.minute,MacTimeRec.second);
  781.         return(timebuffer);
  782.         }
  783.  
  784. void
  785. gnusleep (time)
  786. unsigned time;
  787. {
  788.     long start_t;
  789.     long curr_t;
  790.     EventRecord theEvent;
  791.     int sys;
  792.     start_t = clock() / CLOCKS_PER_SEC;
  793.     for (curr_t = start_t; 
  794.          curr_t - start_t < time;
  795.          curr_t = clock() / CLOCKS_PER_SEC)
  796.         {
  797.             if (WNEIsImplemented())
  798.                 {
  799.                     sys = WaitNextEvent( everyEvent, &theEvent, 
  800.                        (time - (curr_t - start_t)) * CLOCKS_PER_SEC, 0L);
  801.                        HandleEvent(&theEvent, sys);
  802.                        }
  803.                    else
  804.                     {
  805.                         long finalTime;
  806.                         Delay(CLOCKS_PER_SEC*time, &finalTime);
  807.                         }
  808.                }
  809.     }
  810.  
  811. extern int interrupted;
  812. int abort_key_pressed ()
  813. {
  814.     if (interrupted) 
  815.         {
  816.             interrupted = 0;
  817.             logit("Aborting Mac/gnuucp", "Goodbye");
  818.             return(1);
  819.             }
  820.     return(0);
  821.     }
  822.  
  823. #define hiword(x)        (((short *) &(x))[0])
  824. #define loword(x)        (((short *) &(x))[1])
  825. extern MenuHandle appleMenu;
  826.  
  827. void HandleEvent(event, sys)
  828. EventRecord *event;
  829. int sys;
  830. {
  831.     int key;
  832.     WindowPeek wp;
  833.     long choice;
  834.     Str255 buf;
  835.     
  836.         /*  check for an event  */
  837.         
  838.     SEvtEnb = false;
  839.     SystemTask();
  840.     if (sys != 0) {
  841.         if (!SystemEvent(event))
  842.             goto doEvent;
  843.     }
  844.     else if (event->what == nullEvent) {
  845.         if (FrontWindow() == 0)
  846.             InitCursor();
  847.     }
  848.     return;
  849.     
  850.         /*  handle event  */
  851.  
  852. doEvent:
  853.     if (event->what == mouseDown) {
  854.         switch (FindWindow(event->where, &wp)) {
  855.             case inMenuBar:
  856.                 InitCursor();
  857.                 choice = MenuSelect(event->where);
  858.                 goto doMenu;
  859.             case inSysWindow:
  860.                 SystemClick(event, wp);
  861.                 break;
  862.         }
  863.     }
  864.     return;
  865.  
  866.         /*  handle menu choice  */
  867.  
  868. doMenu:    
  869.     switch (hiword(choice)) {
  870.         case 1:        /*  Apple  */
  871.             GetItem(appleMenu, loword(choice), buf);
  872.             OpenDeskAcc(buf);
  873.             break;
  874.         case 2:        /*  File  */
  875.             console_options.pause_atexit = 0;
  876.             exit(0);
  877.             /* no return */
  878.         case 3:        /*  Edit  */
  879.             SystemEdit(loword(choice) - 1);
  880.             break;
  881.     }
  882.     HiliteMenu(0);
  883. }
  884.  
  885.                                     /** Define trap numbers **/
  886. #ifndef _Unimplemented
  887. #define _Unimplemented    0xA89F            /* Unimplemented trap                */
  888. #endif
  889.  
  890. #ifndef _WaitNextEvent
  891. #define _WaitNextEvent    0xA860            /* WaitNextEvent trap                */
  892. #endif
  893.  
  894. #ifndef _OSDispatch
  895. #define _OSDispatch        0xA88F            /* Temporary MF memory calls        */
  896. #endif
  897.  
  898. /******************************************************************************
  899.  TrapAvailable
  900.  
  901.      Check whether a certain trap exists on this machine. For pre-Mac II
  902.      machines, trap numbers only go up to 0x1FF.
  903.  ******************************************************************************/
  904.  
  905. Boolean        TrapAvailable(
  906.     short            trapNum,            /* The trap number to check            */
  907.     short            tType)                /* OS or Toolbox trap                */
  908. {
  909.     SysEnvRec        theWorld;
  910.     
  911.     SysEnvirons(1, &theWorld);
  912.     
  913.     if ( (tType == ToolTrap)  &&                        /* Toolbox trap        */
  914.          ((theWorld.machineType < envMachUnknown)  ||    /* 64K ROM machine    */
  915.          ((theWorld.machineType > envMachUnknown)  &&    /* ╔512KE, Plus, or    */
  916.          (theWorld.machineType < envMacII))) ) {        /* ╔SE                */
  917.          
  918.         trapNum &= 0x3FF;                /* Traps numbers are 10 bits long    */
  919.         
  920.         if (trapNum > 0x1FF) {            /* Traps only go up to 0x1FF on        */
  921.             return(false);                /*   these machines                    */
  922.         }
  923.     }
  924.     
  925.         /* Compare the address of this trap with that of the    */
  926.         /* unimplemented trap                                    */
  927.  
  928.     return( NGetTrapAddress(trapNum, tType)  !=
  929.             GetTrapAddress(_Unimplemented) );
  930. }
  931.  
  932.  
  933. /******************************************************************************
  934.  WNEIsImplemented
  935.  
  936.      Check whether the WaitNextEvent is implemented or not
  937.  ******************************************************************************/
  938.  
  939. Boolean        WNEIsImplemented()
  940. {
  941.     SysEnvRec    theWorld;                /* System environment                */
  942.     
  943.     SysEnvirons(1, &theWorld);            /* Check environment                */
  944.     
  945.     if (theWorld.machineType < 0)         /* Old ROMs, definitely not present    */
  946.         return(FALSE);
  947.         
  948.     else                                /* Check for WNE trap                */
  949.         return(TrapAvailable(_WaitNextEvent, ToolTrap));
  950. }
  951.  
  952.